package com.fs.base.utils;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;


/**
 * @author TiddlerCJ
 * @desc json格式数据返回工具类
 */
public class JsonUtils {
	private static String FLAG = "flag";
	private static String MSG = "msg";
	
	public static String success(String msg){
		JSONObject item = new JSONObject();
		item.put(FLAG, true);item.put(MSG, msg);
		return item.toJSONString();
	}
	
	public static String success(){
		JSONObject item = new JSONObject();
		item.put(FLAG, true);
		return item.toJSONString();
	}
	
	public static String error(String msg){
		JSONObject item = new JSONObject();
		item.put(FLAG, false);item.put(MSG, msg);
		return item.toJSONString();
	}
	
	public static String error(){
		JSONObject item = new JSONObject();
		item.put(FLAG, false);
		return item.toJSONString();
	}
	
	public static PropertyFilter filter(final Map<String, String> fieldsMap){
		 return new PropertyFilter(){
	            public boolean apply(Object obj, String name, Object value) {
	                if(!fieldsMap.containsKey(name)){
	                    return false;
	                }
	                return true;
	            }
	        };
	}
	
	/*public static String toJSONString(Object object, String dateFormat, SerializerFeature... features) {
		return toJSONString(object, dateFormat, new SerializeFilter[]{}, features);
	}

	public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features) {
		return toJSONString(object, null, filter, features);
	}

	public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {
		return toJSONString(object, null, filters, features);
	}

	public static String toJSONString(Object object, String dateFormat, SerializeFilter filter, SerializerFeature... features) {
		return toJSONString(object, dateFormat, new SerializeFilter[]{filter}, features);
	}

	public static String toJSONString(Object object, String dateFormat, SerializeFilter[] filters, SerializerFeature... features) {
		SerializeWriter out = new SerializeWriter();
		try {
			JSONSerializer serializer = new JSONSerializer(out);
			FastJsonConfig config = getFastJsonConfig(dateFormat, filters, features);
			if (ArrayUtils.isNotEmpty(config.getSerializerFeatures())) {
				for (SerializerFeature feature : config.getSerializerFeatures()) {
					serializer.config(feature, true);
				}
			}
			if (StringUtils.isNotBlank(config.getDateFormat())) {
				serializer.setDateFormat(config.getDateFormat());
			}
			if (ArrayUtils.isNotEmpty(config.getSerializeFilters())) {
				for (SerializeFilter filter : config.getSerializeFilters()) {
					serializer.addFilter(filter);
				}
			}
			serializer.write(object);
			return out.toString();
		} finally {
			out.close();
		}
	}

	public static FastJsonData getFastJsonData(Object data, String dateFormat, SerializerFeature... features) {
		return getFastJsonData(data, dateFormat, null, features);
	}

	public static FastJsonData getFastJsonData(Object data, String dateFormat, SerializeFilter[] filters, SerializerFeature... features) {
		return getFastJsonConfig(new FastJsonData(data), dateFormat, filters, features);
	}

	public static FastJsonConfig getFastJsonConfig(String dateFormat, SerializeFilter[] filters, SerializerFeature... features) {
		return getFastJsonConfig(new FastJsonConfig(), dateFormat, filters, features);
	}

	private static <T extends FastJsonConfig> T getFastJsonConfig(T config, String dateFormat, SerializeFilter[] filters, SerializerFeature... features) {
		BeanUtils.copyProperties(fastJsonConfig, config);
		if (ArrayUtils.isNotEmpty(features)) {
			config.setSerializerFeatures(ArrayUtils.addAll(config.getSerializerFeatures(), features));
		}
		if (ArrayUtils.isNotEmpty(filters)) {
			config.setSerializeFilters(filters);
		}
		if (StringUtils.isNotBlank(dateFormat)) {
			config.setDateFormat(dateFormat);
		}
		return config;
	}
	
	private static <T extends FastJsonConfig> T getFastJsonConfig(T config, SerializeFilter[] filters, SerializerFeature... features) {
		BeanUtils.copyProperties(fastJsonConfig, config);
		if (ArrayUtils.isNotEmpty(features)) {
			config.setSerializerFeatures(ArrayUtils.addAll(config.getSerializerFeatures(), features));
		}
		if (ArrayUtils.isNotEmpty(filters)) {
			config.setSerializeFilters(filters);
		}
		return config;
	}
	
	
	public static FastJsonData getFastJsonData(Object data, SerializeFilter[] filters, SerializerFeature... features) {
		return getFastJsonConfig(new FastJsonData(data), filters, features);
	}
	

	private static FastJsonConfig fastJsonConfig = new FastJsonConfig();
	public static SerializeConfig serializeConfig = new SerializeConfig();

	static {
		fastJsonConfig.setSerializerFeatures(SerializerFeature.IgnoreNonFieldGetter, 
				SerializerFeature.DisableCircularReferenceDetect
				, SerializerFeature.WriteDateUseDateFormat);
		//js中无法处理长整形数字，这里全部变为字符串
		serializeConfig.put(Long.class, StringCodec.instance);
		serializeConfig.put(BigInteger.class, StringCodec.instance);
		serializeConfig.put(BigDecimal.class, StringCodec.instance);
	}

	@AllArgsConstructor  @NoArgsConstructor
	public static class FastJsonData extends FastJsonConfig {
		@Getter @Setter private Object value;
	}*/
}
